﻿@using FCTC_Web.Models
@model ImageModel
<script src="@Url.Content("~/Scripts/jquery.form.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/FileUpload.js")" type="text/javascript"></script>
<script type="text/jscript">

    $(document).ready(function () {

        var options = {
            iframe: true,
            success: showResponse,
            target: "#uploadTarget",
            url: "/Upload/AjaxUploadImg",
            type: "post",
            dataType: 'json',
            beforeSubmit: function () {
            }
        };

        var num = $('form').length;
        var from_TargetNum = "";
        for (var i = 0; i < num; i++) {
            $($('form').eq(i)).each(function () {
                if ($(this).find("#file").length) { from_TargetNum = i; }
            })
            if (from_TargetNum != "") break;
        }

        $('form').eq(from_TargetNum).submit(function () {
            return true;
        });

        $("#file").change(function () {
            $('form').eq(from_TargetNum).ajaxSubmit(options);
        });
    })

    function showResponse(response) {
        alert(response.message);
        if (response.status == "sucess") {
            $("#uploadImg").attr('src', response.message);
        }
        else {
            $("#file").replaceWith("<input type='file' id='file' name='file' style='width:300px;' />");


            var options = {
                iframe: true,
                success: showResponse,
                target: "#uploadTarget",
                url: "/Upload/AjaxUploadImg",
                type: "post",
                dataType: 'json',
                beforeSubmit: function () {
                }
            }

            var num = $('form').length;
            var from_TargetNum = "";
            for (var i = 0; i < num; i++) {
                $($('form').eq(i)).each(function () {
                    if ($(this).find("#file").length) { from_TargetNum = i; }
                })
                if (from_TargetNum != "") break;
            }

            $('form').eq(from_TargetNum).submit(function () {
                return true;
            });

            $("#file").change(function () {
                $('form').eq(from_TargetNum).ajaxSubmit(options);
            });
        }
    }



</script>
<div id="uploadTarget">
 <table width="90%" style="background-color:#f6fdfd; "  align="center">
            <tr>
                <td style="width:150px; text-align:right;">
                    <span class="headtitle">请选择你要上传的图片</span>
                </td>
                <td colspan="3">
                    @if (Model != null)
                    { 
                        <span class="headtitles"><img style="width:240px; height:180px;" src=@Url.Action("ShowImage", "Image", new { id = Model.ID }) /></span><br />
                    }
                    else
                    {
                        <span class="headtitles"><img style="width:240px; height:180px;"  id="uploadImg" alt="" src="/images/moren_person.jpg" /></span><br />
                    }
                    <input type="file" id="file" name="file" style="width:240px; border:1px #ccc solid" />
                </td>
            </tr>
            </table>
 
    <br />
</div>
